package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.client.ProductFeignClient;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@SuppressWarnings("all")
public class CartController {

    @Autowired
    private CartFeignClient cartFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    //查看购物车
    @RequestMapping("cart.html")
    public String index(){
        return "cart/index";
    }


    /**
     * 添加商品到购物车
     * @param request
     * @return
     */
    @RequestMapping("addCart.html")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("skuNum") Integer skuNum,
                          HttpServletRequest request
    ){
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);

        request.setAttribute("skuInfo",skuInfo);
        request.setAttribute("skuNum",skuNum);
        return "cart/addCart";
    }
    /*上边方法的另一种方式
    @RequestMapping("addCart.html")
    public String addCart(HttpServletRequest request){

        long skuId = Long.parseLong(request.getParameter("skuId"));
        int skuNum= Integer.parseInt(request.getParameter("skuNum"));

        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        request.setAttribute("skuInfo",skuInfo);
        request.setAttribute("skuNum",skuNum);

        return "cart/addCart";
    }*/

}
